/**
 * 
 */
package com.primestone.cim.batch;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author cesar.mateus
 * Dec 13, 2013 5:19:11 PM
 * Properties.java
 * GetMeterReadingsClient
 * 
 */
public class CIMGMRProperties {
	
	private final static Logger LOGGER = Logger.getLogger(CIMGMRProperties.class.getName());

	private static final String PROP_FILENAME = "properties.properties";
	private static final String WS_LOCATION_KEY = "WS_LOCATION";
	private static final String TRANSFER_KEY = "TRANSFER";
	private static final String FTP_DIR_KEY = "FTP_DIR";
	private static final String FTP_USER_KEY = "FTP_USER";
	private static final String FTP_PWD_KEY = "FTP_PWD";
	private static final String FTP_HOST_KEY = "FTP_HOST";
	private static final String FTP_PORT_KEY = "FTP_PORT";
	private static final String TMP_DIR_KEY = "TMP_DIR";
	private static final String PAGE_SIZE_KEY = "PAGE_SIZE";
	private static final String PROCESS_LP_KEY = "PROCESS_LP";
	private static final String PROCESS_R_KEY = "PROCESS_R";
	private static final String PROCESS_V_KEY = "PROCESS_V";
	private static final String CIM_PWD_KEY = "CIM_PWD";
	private static final String SOURCE_KEY = "SOURCE";
	private static final String ORGANIZATION_KEY = "ORGANIZATION";
	
	public static String WS_LOCATION_PROP;
	public static boolean TRANSFER_PROP;
	public static String FTP_DIR_PROP;
	public static String FTP_USER_PROP;
	public static String FTP_PWD_PROP;
	public static String FTP_HOST_PROP;
	public static int FTP_PORT_PROP;
	public static String TMP_DIR_PROP;
	public static int PAGE_SIZE_PROP;
	public static boolean PROCESS_LP_PROP;
	public static boolean PROCESS_R_PROP;
	public static boolean PROCESS_V_PROP;
	public static String CIM_PWD_PROP;
	public static String SOURCE_PROP;
	public static String ORGANIZATION_PROP;
	
	private static final String DEVICES_FILE = "devices.csv";
	private static List<String> DEVICES_LIST = new ArrayList<String>();
	
	static {
    	Properties prop = new Properties();
		FileInputStream fileInputStream = null;
    	try {
            File propFile = new File(PROP_FILENAME);
    		LOGGER.log(Level.CONFIG, "---- loading in CIMGMRProperties class: {0}", propFile.getAbsolutePath());
    		
    		fileInputStream = new FileInputStream(propFile);
    		prop.load(fileInputStream);
            WS_LOCATION_PROP = prop.getProperty(WS_LOCATION_KEY);
            TRANSFER_PROP = Boolean.parseBoolean(prop.getProperty(TRANSFER_KEY));
            FTP_DIR_PROP = prop.getProperty(FTP_DIR_KEY);
            FTP_USER_PROP = prop.getProperty(FTP_USER_KEY);
            FTP_PWD_PROP = prop.getProperty(FTP_PWD_KEY);
            FTP_HOST_PROP = prop.getProperty(FTP_HOST_KEY);
            FTP_PORT_PROP = Integer.parseInt(prop.getProperty(FTP_PORT_KEY));
            TMP_DIR_PROP = prop.getProperty(TMP_DIR_KEY);
            PAGE_SIZE_PROP = Integer.parseInt(prop.getProperty(PAGE_SIZE_KEY));
            
            PROCESS_LP_PROP = Boolean.parseBoolean(prop.getProperty(PROCESS_LP_KEY));
            PROCESS_R_PROP = Boolean.parseBoolean(prop.getProperty(PROCESS_R_KEY));
            PROCESS_V_PROP = Boolean.parseBoolean(prop.getProperty(PROCESS_V_KEY));
            
            CIM_PWD_PROP = prop.getProperty(CIM_PWD_KEY);
            SOURCE_PROP = prop.getProperty(SOURCE_KEY);
            ORGANIZATION_PROP = prop.getProperty(ORGANIZATION_KEY);
            
            String binDir = propFile.getAbsolutePath().substring(0, propFile.getAbsolutePath().lastIndexOf(File.separator));
            Scanner scanner = new Scanner(new File(binDir + File.separator + DEVICES_FILE));
            
            while (scanner.hasNextLine()) {
            	DEVICES_LIST.add(scanner.next());
            }
            
    	} catch (IOException ex) {
    		ex.printStackTrace();
    	} finally {
    		try {
    			if (fileInputStream != null) {
    				fileInputStream.close();
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
	}
	
	/**
     * @return
     */
    public static List<String> getDevicesList(){
    	return DEVICES_LIST;
    }

}
